home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 December / PCWDEC06.iso / Software / Trial / Paint Shop Pro XI / Data1.cab / _68C726B75EA24F598AA76EF3D50D6EAF < prev    next >
Encoding:
Text File  |  2006-08-04  |  11.0 KB  |  277 lines

  1. from PSPApp import *
  2.  
  3. # Copyright 2005 Corel Software Inc., all rights reserved
  4. # This file contains utility routines provided by Corel Software.
  5. #
  6. #   JascUtils.py - Obsolete - Please use PSPUtils.py
  7. #
  8. #   Utility functions
  9.  
  10. ScriptData = {}
  11.  
  12. # Begin Translatable Strings
  13. SelectionLayer = u"Selectie tot laag gemaakt via script"
  14. Pattern = u"Patroon"
  15. Inline = u"Inline"
  16. Gradient = u"Verloop"
  17. Linear = u"Lineair"
  18. Radial = u"Radiaal"
  19. Rectangular = u"Rechthoekig"
  20. Sunburst = u"Zonnestraal"
  21. Solid = u"Effen"
  22. Null = u"Nul"
  23. Art = u"Illustratie"
  24. RequiresOpenImage = u"Dit script vereist een geopende afbeelding."
  25. # End Translatable Strings
  26.  
  27. class SaveSelection:
  28.     ''' define a helper class that can save any active selection to the alpha
  29.         channel and restore it later
  30.     '''
  31.     def __init__( self, Environment, Doc ):
  32.         ''' at init time we save the environment variable provided by PSP,
  33.             and if a selection exists we save it to an alpha channel
  34.         '''
  35.         self.Env = Environment
  36.         self.SavedSelection =  '__$TempSavedSelection$__'
  37.         self.IsSaved = 0
  38.         self.SavedOnDoc = Doc
  39.         
  40.         SelResult = App.Do( self.Env, 'GetRasterSelectionRect' )
  41.         if SelResult[ 'Type' ] != App.Constants.SelectionType.None:
  42.             # if there is a selection save it to the alpha channel
  43.             App.Do( self.Env, 'SelectSaveDisk', {
  44.                 'FileName': self.SavedSelection, 
  45.                 'Overwrite': App.Constants.Boolean.true, 
  46.                 'GeneralSettings': {
  47.                     'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  48.                     'AutoActionMode': App.Constants.AutoActionMode.Match
  49.                     }
  50.                 }, Doc)
  51.             self.IsSaved = 1    # set this so we know we saved one
  52.             
  53.             if SelResult[ 'Type' ] == App.Constants.SelectionType.Floating:
  54.                 # if the selection is floating promote it to a layer
  55.                 App.Do( self.Env, 'SelectPromote', {
  56.                         'KeepSelection': App.Constants.Boolean.false, 
  57.                         'LayerName': SelectionLayer, 
  58.                         'GeneralSettings': {
  59.                             'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  60.                             'AutoActionMode': App.Constants.AutoActionMode.Match
  61.                             }
  62.                         }, Doc)
  63.             else:
  64.                 App.Do( self.Env, 'SelectNone' )
  65.            
  66.             
  67.     def RestoreSelection( self ):
  68.         ''' if we have saved a selection, restore it now.  If we promoted
  69.             a floating selection to a layer we don't restore the selection
  70.             but don't attempt to mess with the layer in any way
  71.         '''
  72.         if self.IsSaved == 1:
  73.             # load the selection back from disk - this will replace any existing selection
  74.             App.Do( self.Env, 'SelectLoadDisk', {
  75.                     'FileName': self.SavedSelection, 
  76.                     'Operation': App.Constants.SelectionOperation.Replace, 
  77.                     'UpperLeft': App.Constants.Boolean.false, 
  78.                     'ClipToCanvas': App.Constants.Boolean.false, 
  79.                     'GreyMethod': App.Constants.CreateMaskFrom.Luminance, 
  80.                     'Invert': App.Constants.Boolean.false, 
  81.                     'GeneralSettings': {
  82.                         'ExecutionMode': App.Constants.ExecutionMode.Silent, 
  83.                         'AutoActionMode': App.Constants.AutoActionMode.Match
  84.                         }
  85.                     }, self.SavedOnDoc)
  86.  
  87.     # end class SaveSelection
  88.  
  89. def NameFromMaterial( Material, Delimiter=' ', IncludeTexture=1 ):
  90.     ''' Given a material repository, return a name that describes it.
  91.         By default the name is delimited with space, but the delimiter
  92.         parameter can be used to override it.
  93.         By default textures are included in the name, but can be omitted
  94.         by setting the IncludeTexture parameter to 0
  95.     '''
  96.     TextureName = None
  97.     TypeName = ''
  98.     if Material is None:
  99.         CoreName = Null
  100.     else:
  101.         if Material[ 'Pattern' ] and \
  102.            (Material[ 'Pattern' ][ 'Name' ] or Material[ 'Pattern' ][ 'Image' ] ):
  103.             TypeName = Pattern
  104.             if Material[ 'Pattern' ][ 'Name' ]:
  105.                 CoreName = Material[ 'Pattern'  ][ 'Name' ]
  106.             else:
  107.                 CoreName = Inline
  108.         elif Material[ 'Gradient' ] and Material[ 'Gradient' ][ 'Name' ]:
  109.             TypeName = Gradient
  110.             GradType = {}
  111.             GradType[ App.Constants.GradientType.Linear ] = Linear
  112.             GradType[ App.Constants.GradientType.Rectangular ] = Rectangular
  113.             GradType[ App.Constants.GradientType.Radial ] = Radial
  114.             GradType[ App.Constants.GradientType.Angular ] = Sunburst
  115.             
  116.             CoreName = '%s%s%s' % ( Material[ 'Gradient' ][ 'Name' ], Delimiter, 
  117.                                     GradType[ Material[ 'Gradient' ][ 'GradientType' ] ] )
  118.         elif Material[ 'Art' ]:
  119.             TypeName = Art
  120.             CoreName = '%02x%02x%02x' % Material[ 'Art' ][ 'Color' ]
  121.         else:
  122.             TypeName = Solid
  123.             CoreName = '%02x%02x%02x' % Material[ 'Color' ]
  124.  
  125.         if Material[ 'Texture' ] and Material[ 'Texture' ][ 'Name' ]:
  126.             TextureName = Material[ 'Texture' ]['Name']
  127.  
  128.     if TextureName is not None and IncludeTexture != 0:
  129.         MaterialName = '%s%s%s%s%s' % ( TypeName, Delimiter, CoreName, Delimiter, TextureName )
  130.     else:
  131.         MaterialName = '%s%s%s' % ( TypeName, Delimiter, CoreName )
  132.  
  133.     return MaterialName
  134.  
  135. def IsNullMaterial( Material ):
  136.     ' check if the passed in material is none.  Returns true if null, false if non-null'
  137.     if Material is None:    
  138.         return App.Constants.Boolean.true   # material might be entirely none
  139.  
  140.     ArtIsNone = 0
  141.     ColorIsNone = 0
  142.     GradientIsNone = 0
  143.     PatternIsNone = 0
  144.     
  145.     if Material['Color'] is None:
  146.         ColorIsNone = 1
  147.  
  148.     if Material['Gradient'] is None or Material['Gradient']['Name'] is None:
  149.         GradientIsNone = 1
  150.  
  151.     if Material['Pattern'] is None or \
  152.        (Material['Pattern']['Name'] is None and Material['Pattern']['Image'] is None):
  153.         PatternIsNone = 1
  154.  
  155.     if Material['Art'] is None:
  156.         ArtIsNone = 1
  157.  
  158.     if ColorIsNone and GradientIsNone and PatternIsNone and ArtIsNone:
  159.         return App.Constants.Boolean.true   #it works out to none
  160.     else:
  161.         return App.Constants.Boolean.false
  162.  
  163.  
  164. def IsFlatImage( Environment, Doc ):
  165.     'Determine if Doc consists of a single background layer.  True if flat, false if not'
  166.     ImageInfo = App.Do( Environment, 'ReturnImageInfo', {}, Doc )
  167.     LayerInfo = App.Do( Environment, 'ReturnLayerProperties', {}, Doc )
  168.  
  169.     if ImageInfo[ 'LayerNum' ] == 1 and LayerInfo[ 'IsBackground' ] == App.Constants.Boolean.true:
  170.         return App.Constants.Boolean.true
  171.     else:
  172.         return App.Constants.Boolean.false
  173.  
  174. def IsPaletted( Environment, Doc ):
  175.     '''Determine if the current image is paletted.  Greyscale is not counted as paletted
  176.        Returns true on paletted, false if not
  177.     '''
  178.     # these are all the paletted pixel formats
  179.     TargetFormats = [ App.Constants.PixelFormat.Index1, App.Constants.PixelFormat.Index4,
  180.                       App.Constants.PixelFormat.Index8 ]
  181.     
  182.     # are we paletted?
  183.     Info = App.Do( Environment, 'ReturnImageInfo', {}, Doc )
  184.  
  185.     if Info['PixelFormat'] in TargetFormats:
  186.         return App.Constants.Boolean.true
  187.     else:
  188.         return App.Constants.Boolean.false
  189.  
  190. def IsTrueColor( Environment, Doc ):
  191.     ''' Determine if the current image is true color.  Greyscale does not count
  192.         Returns true for true color, false for all others
  193.     '''
  194.     TargetFormats = [ App.Constants.PixelFormat.BGR, App.Constants.PixelFormat.BGRA ]
  195.     
  196.     # are we true color?
  197.     Info = App.Do( Environment, 'ReturnImageInfo', {}, Doc )
  198.     if Info['PixelFormat'] in TargetFormats:
  199.         return App.Constants.Boolean.true
  200.     else:
  201.         return App.Constants.Boolean.false
  202.  
  203. def IsGreyScale( Environment, Doc ):
  204.     ' Determine if the current image (not layer) is greyscale. True if it is, false otherwise'
  205.     TargetFormats = [ App.Constants.PixelFormat.Grey, App.Constants.PixelFormat.GreyA ]
  206.     
  207.     # are we true color?
  208.     Info = App.Do( Environment, 'ReturnImageInfo', {}, Doc )
  209.     if Info['PixelFormat'] in TargetFormats:
  210.         return App.Constants.Boolean.true
  211.     else:
  212.         return App.Constants.Boolean.false
  213.  
  214. def LayerIsArtMedia( Environment, Doc ):
  215.     'Returns true if the current layer is a artmedia layer'
  216.     LayerInfo = App.Do( Environment, 'ReturnLayerProperties', {}, Doc )
  217.     if LayerInfo[ 'LayerType' ] == App.Constants.LayerType.ArtMedia:
  218.         return App.Constants.Boolean.true
  219.     else:
  220.         return App.Constants.Boolean.false
  221.  
  222. def LayerIsRaster( Environment, Doc ):
  223.     'Returns true if the current layer is a raster layer'
  224.     LayerInfo = App.Do( Environment, 'ReturnLayerProperties', {}, Doc )
  225.     if LayerInfo[ 'LayerType' ] == App.Constants.LayerType.Raster:
  226.         return App.Constants.Boolean.true
  227.     else:
  228.         return App.Constants.Boolean.false
  229.  
  230.  
  231. def LayerIsVector( Environment, Doc ):
  232.     'Returns true if the current layer is a vector layer'
  233.     LayerInfo = App.Do( Environment, 'ReturnLayerProperties', {}, Doc )
  234.     if LayerInfo[ 'LayerType' ] == App.Constants.LayerType.Vector:
  235.         return App.Constants.Boolean.true
  236.     else:
  237.         return App.Constants.Boolean.false
  238.  
  239. def LayerIsBackground( Environment, Doc ):
  240.     'Returns true if the current layer is the background layer'
  241.     LayerInfo = App.Do( Environment, 'ReturnLayerProperties', {}, Doc )
  242.     return LayerInfo[ 'IsBackground' ]
  243.     
  244.  
  245. def GetLayerCount( Environment, Doc ):
  246.     'Returns number of layers in Doc'
  247.     ImageInfo = App.Do( Environment, 'ReturnImageInfo', {}, Doc )
  248.     return ImageInfo[ 'LayerNum' ]    
  249.  
  250. def GetCurrentLayerName( Environment, Doc ):
  251.     'Returns the name of the current layer in Doc'
  252.     LayerInfo = App.Do( Environment, 'ReturnLayerProperties', {}, Doc )
  253.     return LayerInfo[ 'General' ][ 'Name' ]
  254.  
  255. def PromoteToTrueColor( Environment, Doc ):
  256.     'If the current image type is paletted, promote it to true color.  Greyscale is left alone'
  257.     if IsPaletted( Environment, Doc ):
  258.         App.Do( Environment, 'IncreaseColorsTo16Million', {}, Doc )
  259.     return
  260.  
  261. def RequireADoc( Environment ):
  262.     '''Test that we actually have a target document, and put up a message box if we dont
  263.        Returns true if we have an open doc, false otherwise
  264.     '''
  265.     if App.TargetDocument is None:
  266.         App.Do( Environment, 'MsgBox', {
  267.                 'Buttons': App.Constants.MsgButtons.OK, 
  268.                 'Icon': App.Constants.MsgIcons.Stop, 
  269.                 'Text': RequiresOpenImage, 
  270.                 })
  271.         return App.Constants.Boolean.false
  272.     else:
  273.         return App.Constants.Boolean.true
  274.  
  275.     
  276.         
  277.